package DBLayer;

import java.sql.*;

/**
 * @author Kis Boisen Hansen
 * @version 2006.10.02 - revised marts 2009 FEN
 * @version 2010.10.07 - new connection 
 * @version 2011.11.02 - new methods to handle transaction
 * @version 2013.18.10 - Rewamp of constructor + cleanup of constants by Jan Skrubbeltrang
 * @version 2013.19.12 - Added Exception handling. 
 * This class is used to create the connection to the database
 * It is implementet as a singelton. The constructor is private to encure that only
 * one object of the class is generated
 * Version for Sql Server or MySql lines in comments
 */

public class DBConnection {

	//constans used to get access to the database
	//SQL Server
	private static String userName="dmaa0213_2";
	private static String password = "MaaGodt";
	private static String dbms = "sqlserver://balder.ucn.dk:1433";
	private static String database = "dmaa0213_2";
	private static String url = "jdbc:" + dbms + 
			";databaseName=" + database +
			";user=" + userName +
			";password=" + password;

	private DatabaseMetaData dma;
	private static Connection con;

	// an instance of the class is generetated
	private static DBConnection  instance = null;

	// the constructor is private to ensure that only one object of this class is createt
	private DBConnection() throws SQLException
	{
		try{
			con = DriverManager.getConnection(url);
			con.setAutoCommit(true);
			dma = con.getMetaData(); // get meta data
			System.out.println("Connection to " + dma.getURL());
			System.out.println("Driver " + dma.getDriverName());
			System.out.println("Database product name " + dma.getDatabaseProductName());
		}
		catch(SQLException e){
			System.out.println("Problems with the connection to the database");
			System.out.println(e.getMessage());
			System.out.println(url);
			throw e;
		}
	}

	//closeDb: closes the connection to the database
	public static void closeConnection()
	{
		try{
			con.close();
			System.out.println("The connection is closed");
		}
		catch (Exception e){
			System.out.println("Error trying to close the database " +  e.getMessage());
		}
	}//end closeDB

	//getDBcon: Get-metode, returnerer forbindelsen til databasen
	public  Connection getDBcon()
	{
		return con;
	}
	//this method is used to get the instance of the connection
	public static DBConnection getInstance() throws SQLException
	{
		if (instance == null)
		{
			try {
				instance = new DBConnection();
			}
			catch (SQLException e) {
				throw e;
			}
		}
		return instance;
	}
	public static void startTransaction()
	{ 
		try {
			con.setAutoCommit(false);
		}
		catch(Exception e){
			System.out.println("fejl start transaction");
			System.out.println(e.getMessage());
		}
	}
	public static void commitTransaction()
	{
		try{
			con.setAutoCommit(true);
		}
		catch(Exception e){
			System.out.println("fejl commit transaction");
			System.out.println(e.getMessage());
		}
	}
	public static void rollbackTransaction()
	{
		try{
			con.rollback();
			con.setAutoCommit(true);
		}
		catch(Exception e){
			System.out.println("fejl rollback transaction");
			System.out.println(e.getMessage());
		}
	}
}
